﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _15Puzzle_GUI
{
    public partial class FormPuzzle : Form
    {
        #region MemberData
        private FifteenPuzzle puzzle = new FifteenPuzzle();
        private Rectangle hitBox = new Rectangle();

        #endregion

        #region FormSpecificFunctions
        public FormPuzzle()
        {
            InitializeComponent();
            puzzle.TileHeight = this.Height / 4 - 10;
            puzzle.TileWidth = this.Width / 4 - 5;
            hitBox.Width = puzzle.TileWidth;
            hitBox.Height = puzzle.TileHeight;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void FormPuzzle_Paint(object sender, PaintEventArgs e)
        {
            puzzle.DrawBoard(this.CreateGraphics());
        }

        private void FormPuzzle_ResizeEnd(object sender, EventArgs e)
        {
            puzzle.TileHeight = this.Height / 4 - 10;
            puzzle.TileWidth = this.Width / 4 - 5;
            hitBox.Width = puzzle.TileWidth;
            hitBox.Height = puzzle.TileHeight;
            this.Invalidate();
        } 
        #endregion

        #region InputHandlers
        private void FormPuzzle_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down && puzzle.FreeX > 0)
            {
                this.Invalidate(puzzle.MoveTiles(-1, 0));
            }
            else if (e.KeyCode == Keys.Left && puzzle.FreeY < 3)
            {
                this.Invalidate(puzzle.MoveTiles(0, 1));
            }
            else if (e.KeyCode == Keys.Right && puzzle.FreeY > 0)
            {
                this.Invalidate(puzzle.MoveTiles(0, -1));
            }
            else if (e.KeyCode == Keys.Up && puzzle.FreeX < 3)
            {
                this.Invalidate(puzzle.MoveTiles(1, 0));
            }
            if (puzzle.Complete)
            {
                MessageBox.Show("Puzzle completed!\n" + "It took you " + puzzle.Moves + " moves.", "Game completed",
                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                puzzle.GenereateBoard();
                this.Invalidate();
            }
        }

        private void FormPuzzle_MouseClick(object sender, MouseEventArgs e)
        {
            bool hitFound = false;
            hitBox.Y = puzzle.FreeX * puzzle.TileWidth;
            //Check Horizontal line
            for (int i = 0; i < 4; i++)
            {
                hitBox.X = i * puzzle.TileHeight;
                if (hitBox.Contains(e.X, e.Y))
                {
                    int loopFor = Math.Abs(i - puzzle.FreeY);
                    for (int j = 0; j < loopFor; j++)
                    {
                        this.Invalidate(puzzle.MoveTiles(0, Math.Sign(i - puzzle.FreeY) * 1));
                    }
                    hitFound = true;
                    break;
                }
            }
            //Check Vertical line
            if (!hitFound)
            {
                hitBox.X = puzzle.FreeY * puzzle.TileHeight;
                for (int i = 0; i < 4; i++)
                {
                    hitBox.Y = i * puzzle.TileHeight;
                    if (hitBox.Contains(e.X, e.Y))
                    {
                        int loopFor = Math.Abs(i - puzzle.FreeX);
                        for (int j = 0; j < loopFor; j++)
                        {
                            this.Invalidate(puzzle.MoveTiles(Math.Sign(i - puzzle.FreeX) * 1, 0));
                        }
                        break;
                    }
                }
            }
            if (puzzle.Complete)
            {
                MessageBox.Show("Puzzle completed!\n" + "It took you " + puzzle.Moves + " moves.", "Game completed",
                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                puzzle.GenereateBoard();
                this.Invalidate();
            }
        } 
        #endregion
    }
}
